package main

import (
	"fmt"
	"time"
)

func worker(done chan bool) {
	fmt.Println("working...")
	time.Sleep(5 * time.Second)
	fmt.Println("done")
	done <- true
}

func ping(ping chan<- string, msg string) {
	ping <- msg
}

func pong(ping <-chan string, pong chan<- string) {
	msg := <-ping
	pong <- msg
}

//channel_buffering
func main() {
	messages := make(chan string, 2)
	messages <- "buffered"
	messages <- "channel"
	fmt.Println(<-messages)
	fmt.Println(<-messages)

	done := make(chan bool)
	go worker(done)
	flag := <-done
	fmt.Println("flag:", flag)

	//通道作参数使用
	pi := make(chan string, 1)
	po := make(chan string, 1)
	go ping(pi, "push message")
	go pong(pi, po)
	fmt.Println("pong:", <-po)
	fmt.Scanln()
	fmt.Println("End")
}
